<?php
namespace app\api\controller\v1;

use app\api\model\UserScheduling;
use app\api\model\SchedulingDetail;
use app\api\model\Image;
use app\api\model\Ticket;
use app\api\service\TokenService;
use app\api\validate\Scheduling as SchedulingValidate;
use app\api\validate\Schedulingdetail as SchedulingdetailValidate;
use app\api\validate\IDPostiveInt;
use app\api\validate\PIDPostiveInt;
use app\lib\exception\BaseException;
use app\lib\exception\SuccessMessage;

//用户行程管理
class Scheduling extends Base
{
    //TP5的前置方法
    protected $beforeActionList = [
        //'checkScope' => ['only' => 'createOrUpdateAddress']
        'checkScope'
    ];

    //我的行程列表
    public function ListInfo(){//0:签证，1：机票,2：行程，3：酒店，4：租车，5：门票，6：wifi
        $uid = TokenService::getCurrentUid();//接收token，获取uid
        $data['0'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'1'])->count();//签证
        $data['1'] = (new Ticket)->where('user_id',$uid)->count();//机票
        $data['2'] = (new UserScheduling)->where('user_id',$uid)->count();//行程
        $data['3'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'2'])->count();//酒店
        $data['4'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'6'])->count();//驾照认证
        $data['5'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'3'])->count();//租车
        $data['6'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'4'])->count();//门票
        $data['7'] = (new Image)->where(['user_id'=>$uid,'img_type'=>'5'])->count();//wifi
        return json($data);
    }

    // 创建用户行程安排
    public function CreateScheduling()
    {
        $validate = new SchedulingValidate();
        $validate->goCheck();
        $post = input('post.');//过滤非验证器中的字段
        if(isset($post['user_id']))
            $uid = '0';
        else $uid = TokenService::getCurrentUid();//接收token，获取uid
        return UserScheduling::CreateOne($uid,$post);
    }

    //获取用户所有行程安排
    public function getUserScheduling()
    {
        $uids[] = '0';
        $uids[] = TokenService::getCurrentUid();//接收token，获取uid
        $data= (new UserScheduling)->where('user_id','in',$uids)->order(['user_id'=>'asc','create_time'=>'desc'])->select();
        if(!$data->isEmpty())
            foreach ($data as &$value){
                $value['during'] = date("m月d日",strtotime($value['start_time'])).'-'.date("m月d日",strtotime($value['end_time']));
                unset($value['start_time']);
                unset($value['end_time']);
            }
        if(!$data){
            throw new BaseException(['msg'=>'获取用户地址失败']);
        }
        return $data;
    }

    //cms获取用户所有行程安排
    public function getReferScheduling()
    {
        $data= (new UserScheduling)->where(['user_id'=>'0'])->order(['user_id'=>'asc','create_time'=>'desc'])->select();
        if(!$data->isEmpty())
            foreach ($data as &$value){
                $value['during'] = date("m月d日",strtotime($value['start_time'])).'-'.date("m月d日",strtotime($value['end_time']));
                unset($value['start_time']);
                unset($value['end_time']);
            }
        if(!$data){
            throw new BaseException(['msg'=>'获取用户地址失败']);
        }
        return $data;
    }

    //删除所有行程安排
    public function DelAllScheduling($type=''){
        if($type == '2'){
            $uid=TokenService::getCurrentUid();
            return UserScheduling::scheduling_all_del($uid);
        }
    }

    //删除指定地址
    public function DelScheduling($id='')
    {
        (new IDPostiveInt)->goCheck();
        $uid=TokenService::getCurrentUid();
        return UserScheduling::scheduling_del($id,$uid);
    }

    //编辑添加具体的行程安排
    public function add_scheduling_detail(){
        $validate = new SchedulingdetailValidate();
        $validate->goCheck();
        $post = $validate->getDataByRule(input('post.'));//过滤非验证器中的字段
        return SchedulingDetail::CreateOne($post);
    }

    //列表详情列表
    public function list_detail($pid = ''){
        (new PIDPostiveInt)->goCheck();
        $data = SchedulingDetail::where(['pid'=>$pid])->order('create_time','asc')->select();
        $data = $data->toArray();
        foreach ($data as &$value){
            $value['during_detail'] = $value['start_time'].'---'.$value['end_time'];
            $value['start_time'] = date("m月d日",strtotime($value['start_time']));
            $value['end_time'] = date("m月d日",strtotime($value['end_time']));
            $value['during'] = $value['start_time'].'-'.$value['end_time'];
        }
        return json($data);
    }

    //具体行程安排删除
    public function scheduling_del($id=''){
        (new IDPostiveInt)->goCheck();
        return SchedulingDetail::scheduling_detail_del($id);
    }

    //一键复制参考
    public function scheduling_refer_add($id=''){
        (new IDPostiveInt)->goCheck();
        $uid=TokenService::getCurrentUid();
        return UserScheduling::scheduling_refer_add($id,$uid);
    }
}
